
<!DOCTYPE HTML>
<html lang="en" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>smart-socket实现RPC · smartboot 中文指南</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../firstfit/readme.html" />
    
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"buttons":[{"user":"smartboot","repo":"smart-socket","type":"star","size":"small","count":true}]};
    </script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    关于smartboot
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    smart-socket 使用手册
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../../smart-socket/">
            
                <a href="../../smart-socket/">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" >
            
                <span>
            
                    
                    第一章 smart-socket开发指南
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.2.1" data-path="../../smart-socket/chapter-1/2.1-基础应用/">
            
                <a href="../../smart-socket/chapter-1/2.1-基础应用/">
            
                    
                    1.1 快速上手
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.2" data-path="../../smart-socket/chapter-1/2.2-Spring集成/">
            
                <a href="../../smart-socket/chapter-1/2.2-Spring集成/">
            
                    
                    1.2 Spring集成smart-socket
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.3" data-path="../../smart-socket/chapter-1/SUMMARY.html">
            
                <a href="../../smart-socket/chapter-1/SUMMARY.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../../smart-socket/chapter-2/">
            
                <a href="../../smart-socket/chapter-2/">
            
                    
                    第二章 源码解析
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.3.1" data-path="../../smart-socket/chapter-2/核心接口/">
            
                <a href="../../smart-socket/chapter-2/核心接口/">
            
                    
                    2.1 核心接口与状态机
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.2" data-path="../../smart-socket/chapter-2/服务端/">
            
                <a href="../../smart-socket/chapter-2/服务端/">
            
                    
                    2.2 服务端
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.3" data-path="../../smart-socket/chapter-2/客户端/">
            
                <a href="../../smart-socket/chapter-2/客户端/">
            
                    
                    2.3 客户端
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.4" data-path="../../smart-socket/chapter-2/AioSession/">
            
                <a href="../../smart-socket/chapter-2/AioSession/">
            
                    
                    2.4 会话AioSession
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.5" data-path="../../smart-socket/chapter-2/SUMMARY.html">
            
                <a href="../../smart-socket/chapter-2/SUMMARY.html">
            
                    
                    2.5 小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../../smart-socket/chapter-3/">
            
                <a href="../../smart-socket/chapter-3/">
            
                    
                    第三章 通信协议
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.4.1" data-path="../../smart-socket/chapter-3/1-消息归类/">
            
                <a href="../../smart-socket/chapter-3/1-消息归类/">
            
                    
                    3.1 消息归类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.2" data-path="../../smart-socket/BLANK.html">
            
                <a href="../../smart-socket/BLANK.html">
            
                    
                    3.2 半包/粘包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.3" data-path="../../smart-socket/BLANK.html">
            
                <a href="../../smart-socket/BLANK.html">
            
                    
                    3.3 Http消息解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.4" data-path="../../smart-socket/BLANK.html">
            
                <a href="../../smart-socket/BLANK.html">
            
                    
                    3.4 RPC消息解析
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../../smart-socket/chapter-5/">
            
                <a href="../../smart-socket/chapter-5/">
            
                    
                    第四章 内存池
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../../smart-socket/chapter-6/">
            
                <a href="../../smart-socket/chapter-6/">
            
                    
                    第五章 未来规划
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../../smart-socket/end/">
            
                <a href="../../smart-socket/end/">
            
                    
                    附录
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    smart-http 实战手册
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../../smart-http/">
            
                <a href="../../smart-http/">
            
                    
                    关于smart-http
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" >
            
                <span>
            
                    
                    第一章 smart-http开发指南
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.2.1" data-path="../../smart-http/chapter-1/">
            
                <a href="../../smart-http/chapter-1/">
            
                    
                    1.1 快速上手
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.2" data-path="../../smart-http/chapter-1/http_route.html">
            
                <a href="../../smart-http/chapter-1/http_route.html">
            
                    
                    1.2 请求路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.3" data-path="../../smart-http/chapter-1/file_upload.html">
            
                <a href="../../smart-http/chapter-1/file_upload.html">
            
                    
                    1.3 文件上传/下载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.4" data-path="../../smart-http/chapter-1/websocket.html">
            
                <a href="../../smart-http/chapter-1/websocket.html">
            
                    
                    1.4 WebSocket
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    技术分享
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.4.1" data-path="smart-socket-rpc.html">
            
                <a href="smart-socket-rpc.html">
            
                    
                    smart-socket实现RPC
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../firstfit/readme.html">
            
                <a href="../firstfit/readme.html">
            
                    
                    内存管理-首次适应算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../thread/readme.html">
            
                <a href="../thread/readme.html">
            
                    
                    Thread知多少
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../../donation.html">
            
                <a href="../../donation.html">
            
                    
                    关于捐赠
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >smart-socket实现RPC</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div class="search-plus" id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="smart-socket&#x5B9E;&#x73B0;rpc">smart-socket&#x5B9E;&#x73B0;RPC</h1>
<p>RPC&#x662F;&#x76EE;&#x524D;&#x88AB;&#x5E7F;&#x6CDB;&#x5E94;&#x7528;&#x4E8E;&#x4E92;&#x8054;&#x7F51;&#x670D;&#x52A1;&#x7684;&#x4E00;&#x9879;&#x6280;&#x672F;&#xFF0C;&#x5173;&#x4E8E;&#x5B83;&#x7684;&#x57FA;&#x672C;&#x4ECB;&#x7ECD;&#x5927;&#x5BB6;&#x53EF;&#x901A;&#x8FC7;&#x767E;&#x5EA6;&#x4E86;&#x89E3;&#x4E00;&#x4E0B;&#xFF0C;&#x6B64;&#x5904;&#x4E0D;&#x518D;&#x8D58;&#x8FF0;&#x3002;
&#x6B63;&#x6240;&#x8C13;&#x8BFB;&#x4E07;&#x5377;&#x4E66;&#x4E0D;&#x5982;&#x884C;&#x4E07;&#x91CC;&#x8DEF;&#xFF0C;&#x539F;&#x7406;&#x6027;&#x7684;&#x6587;&#x7AE0;&#x770B;&#x7684;&#x518D;&#x591A;&#x90FD;&#x4E0D;&#x5982;&#x4EB2;&#x81EA;&#x5B9E;&#x73B0;&#x4E00;&#x904D;RPC&#xFF0C;&#x65B9;&#x53EF;&#x5BF9;&#x5176;&#x4E86;&#x89E3;&#x7684;&#x66F4;&#x52A0;&#x900F;&#x5F7B;&#x3002;
&#x672C;&#x6587;&#x5C06;&#x4EE5;&#x7EAF;&#x6280;&#x672F;&#x89C6;&#x89D2;&#xFF0C;&#x4E3A;&#x5927;&#x5BB6;&#x6F14;&#x793A;&#x4E00;&#x4E0B;RPC&#x7684;&#x5DE5;&#x4F5C;&#x539F;&#x7406;&#x4E0E;&#x5B9E;&#x73B0;&#x65B9;&#x6848;&#x3002;</p>
<p>&#x6B63;&#x5F0F;&#x5F00;&#x59CB;&#x4E4B;&#x524D;&#xFF0C;&#x5148;&#x7F57;&#x5217;&#x4E00;&#x4E0B;&#x5B9E;&#x73B0;RPC&#x9700;&#x8981;&#x8FD0;&#x7528;&#x5230;&#x7684;&#x6280;&#x672F;&#x70B9;&#xFF1A;</p>
<ol>
<li>&#x901A;&#x4FE1;</li>
<li>&#x5E8F;&#x5217;&#x5316;/&#x53CD;&#x5E8F;&#x5217;&#x5316;</li>
<li>&#x53CD;&#x5C04;</li>
<li>&#x52A8;&#x6001;&#x4EE3;&#x7406;</li>
</ol>
<p>&#x5728;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x4E0A;&#x9664;&#x4E86;&#x901A;&#x4FE1;&#x90E8;&#x5206;&#x6211;&#x4EEC;&#x9009;&#x7528;smart-socket&#x6765;&#x8F85;&#x52A9;&#xFF0C;&#x5176;&#x4F59;&#x5305;&#x62EC;&#x5E8F;&#x5217;&#x5316;/&#x53CD;&#x5E8F;&#x5217;&#x5316;&#x3001;&#x53CD;&#x5C04;&#x3001;&#x52A8;&#x6001;&#x4EE3;&#x7406;&#x7B49;&#x90E8;&#x5206;&#x6211;&#x4EEC;&#x5C06;&#x91C7;&#x7528;JDK&#x63D0;&#x4F9B;&#x7684;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#xFF0C;&#x5F85;&#x60A8;&#x638C;&#x63E1;RPC&#x540E;&#x53EF;&#x518D;&#x5C1D;&#x8BD5;&#x7ED3;&#x5408;&#x7B2C;&#x4E09;&#x65B9;&#x6280;&#x672F;&#x6765;&#x91CD;&#x6784;RPC&#x3002;</p>
<h2 id="&#x540D;&#x8BCD;&#x89E3;&#x91CA;">&#x540D;&#x8BCD;&#x89E3;&#x91CA;</h2>
<ul>
<li>Provider<br>  RPC&#x670D;&#x52A1;&#x63D0;&#x4F9B;&#x8005;</li>
<li>Consumer<br>  RPC&#x670D;&#x52A1;&#x8C03;&#x7528;&#x8005;</li>
</ul>
<h2 id="&#x6D88;&#x606F;&#x901A;&#x4FE1;">&#x6D88;&#x606F;&#x901A;&#x4FE1;</h2>
<p>&#x65E2;&#x7136;RPC&#x662F;&#x8DE8;&#x7F51;&#x7EDC;&#x901A;&#x4FE1;&#x670D;&#x52A1;&#xFF0C;&#x90A3;&#x6211;&#x4EEC;&#x5148;&#x5236;&#x5B9A;&#x901A;&#x4FE1;&#x89C4;&#x5219;&#xFF0C;&#x8BE5;&#x90E8;&#x5206;&#x7684;&#x5185;&#x5BB9;&#x6D89;&#x53CA;&#x5230;&#x901A;&#x4FE1;&#x3001;&#x5E8F;&#x5217;&#x5316;/&#x53CD;&#x5E8F;&#x5217;&#x5316;&#x6280;&#x672F;&#x3002;</p>
<h3 id="&#x901A;&#x4FE1;&#x534F;&#x8BAE;">&#x901A;&#x4FE1;&#x534F;&#x8BAE;</h3>
<p>&#x901A;&#x4FE1;&#x534F;&#x8BAE;&#x6211;&#x4EEC;&#x91C7;&#x7528;&#x6700;&#x7B80;&#x5355;&#x7684;length+data&#x6A21;&#x5F0F;&#xFF0C;&#x7F16;&#x89E3;&#x7801;&#x7684;&#x5B9E;&#x73B0;&#x7B97;&#x6CD5;&#x5982;&#x4E0B;&#x3002;
&#x4F5C;&#x4E3A;&#x793A;&#x4F8B;&#x6211;&#x4EEC;&#x5047;&#x8BBE;readBuffer&#x8DB3;&#x591F;&#x5BB9;&#x7EB3;&#x4E00;&#x4E2A;&#x5B8C;&#x6574;&#x7684;&#x6D88;&#x606F;&#xFF0C;&#x534F;&#x8BAE;&#x4E2D;&#x7684;data&#x90E8;&#x5206;&#x4FBF;&#x662F;RPC&#x670D;&#x52A1;&#x5E8F;&#x5217;&#x5316;&#x540E;&#x7684;byte&#x6570;&#x7EC4;&#xFF0C;Provider/Consumer&#x5219;&#x5FC5;&#x987B;&#x5BF9;byte&#x6570;&#x7EC4;&#x5B8C;&#x6210;&#x53CD;&#x5E8F;&#x5217;&#x5316;&#x540E;&#x624D;&#x80FD;&#x7EE7;&#x7EED;RPC&#x670D;&#x52A1;&#x5904;&#x7406;&#x3002;</p>
<pre><code class="lang-java">public class RpcProtocol implements Protocol&lt;byte[]&gt; {
    private static final int INTEGER_BYTES = Integer.SIZE / Byte.SIZE;

    @Override
    public byte[] decode(ByteBuffer readBuffer, AioSession&lt;byte[]&gt; session, boolean eof) {
        int remaining = readBuffer.remaining();
        if (remaining &lt; INTEGER_BYTES) {
            return null;
        }
        int messageSize = readBuffer.getInt(readBuffer.position());
        if (messageSize &gt; remaining) {
            return null;
        }
        byte[] data = new byte[messageSize - INTEGER_BYTES];
        readBuffer.getInt();
        readBuffer.get(data);
        return data;
    }

    @Override
    public ByteBuffer encode(byte[] msg, AioSession&lt;byte[]&gt; session) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(msg.length + INTEGER_BYTES);
        byteBuffer.putInt(byteBuffer.capacity());
        byteBuffer.put(msg);
        byteBuffer.flip();
        return byteBuffer;
    }
}
</code></pre>
<h3 id="rpc&#x8BF7;&#x6C42;&#x6D88;&#x606F;">RPC&#x8BF7;&#x6C42;&#x6D88;&#x606F;</h3>
<p>RPC&#x8BF7;&#x6C42;&#x6D88;&#x606F;&#x7531;Consumer&#x53D1;&#x9001;&#xFF0C;Consumer&#x9700;&#x8981;&#x5728;&#x8BF7;&#x6C42;&#x6D88;&#x606F;&#x4E2D;&#x63D0;&#x4F9B;&#x8DB3;&#x591F;&#x4FE1;&#x606F;&#x4EE5;&#x4F9B;Provider&#x51C6;&#x786E;&#x8BC6;&#x522B;&#x670D;&#x52A1;&#x63A5;&#x53E3;&#x3002;&#x6838;&#x5FC3;&#x8981;&#x7D20;&#x5305;&#x62EC;&#xFF1A;</p>
<ol>
<li>uuid<br> &#x8BF7;&#x6C42;&#x6D88;&#x606F;&#x552F;&#x4E00;&#x6807;&#x8BC6;&#xFF0C;&#x7528;&#x4E8E;&#x5173;&#x8054;&#x3001;&#x8BC6;&#x522B;&#x54CD;&#x5E94;&#x6D88;&#x606F;&#x3002;</li>
<li>interfaceClass<br> Consumer&#x8981;&#x8C03;&#x7528;&#x7684;API&#x63A5;&#x53E3;&#x540D;</li>
<li>method<br> Consumer&#x8981;&#x6267;&#x884C;&#x7684;API&#x63A5;&#x53E3;&#x65B9;&#x6CD5;&#x540D;</li>
<li>paramClassList<br> Consumer&#x8C03;&#x7528;&#x7684;&#x65B9;&#x6CD5;&#x5165;&#x53C2;&#x7C7B;&#x578B;&#xFF0C;&#x7528;&#x4E8E;&#x533A;&#x5206;&#x540C;&#x65B9;&#x6CD5;&#x540D;&#x4E0D;&#x540C;&#x5165;&#x53C2;&#x7684;&#x60C5;&#x51B5;</li>
<li>params<br> Consumer&#x6267;&#x884C;&#x65B9;&#x6CD5;&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;&#x503C;</li>
</ol>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RpcRequest</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Serializable</span> </span>{

    <span class="hljs-comment">/**
     * &#x6D88;&#x606F;&#x7684;&#x552F;&#x4E00;&#x6807;&#x8BC6;
     */</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> String uuid = UUID.randomUUID().toString();

    <span class="hljs-comment">/**
     * &#x63A5;&#x53E3;&#x540D;&#x79F0;
     */</span>
    <span class="hljs-keyword">private</span> String interfaceClass;

    <span class="hljs-comment">/**
     * &#x8C03;&#x7528;&#x65B9;&#x6CD5;
     */</span>
    <span class="hljs-keyword">private</span> String method;

    <span class="hljs-comment">/**
     * &#x53C2;&#x6570;&#x7C7B;&#x578B;&#x5B57;&#x7B26;&#x4E32;
     */</span>
    <span class="hljs-keyword">private</span> String[] paramClassList;

    <span class="hljs-comment">/**
     * &#x5165;&#x53C2;
     */</span>
    <span class="hljs-keyword">private</span> Object[] params;

    getX/setX()
}
</code></pre>
<h3 id="rpc&#x54CD;&#x5E94;&#x6D88;&#x606F;">RPC&#x54CD;&#x5E94;&#x6D88;&#x606F;</h3>
<p>RPC&#x54CD;&#x5E94;&#x6D88;&#x606F;&#x4E3A;Provider&#x5C06;&#x63A5;&#x53E3;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x54CD;&#x5E94;&#x7ED9;Consumer&#x7684;&#x8F7D;&#x4F53;&#x3002;</p>
<ul>
<li>uuid<br>  &#x4E0E;RPC&#x8BF7;&#x6C42;&#x6D88;&#x606F;&#x540C;&#x503C;</li>
<li>returnObject<br>  RPC&#x63A5;&#x53E3;&#x6267;&#x884C;&#x8FD4;&#x56DE;&#x503C;</li>
<li>returnType<br>  RPC&#x63A5;&#x53E3;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;</li>
<li>exception<br>  RPC&#x6267;&#x884C;&#x5F02;&#x5E38;&#x4FE1;&#x606F;&#xFF0C;&#x5982;&#x679C;&#x51FA;&#x73B0;&#x5F02;&#x5E38;&#x7684;&#x8BDD;&#x3002;</li>
</ul>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RpcResponse</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Serializable</span> </span>{
    <span class="hljs-comment">/**
     * &#x6D88;&#x606F;&#x7684;&#x552F;&#x4E00;&#x6807;&#x793A;&#xFF0C;&#x4E0E;&#x5BF9;&#x5E94;&#x7684;RpcRequest uuid&#x503C;&#x76F8;&#x540C;
     */</span>
    <span class="hljs-keyword">private</span> String uuid;
    <span class="hljs-comment">/**
     * &#x8FD4;&#x56DE;&#x5BF9;&#x8C61;
     */</span>
    <span class="hljs-keyword">private</span> Object returnObject;

    <span class="hljs-comment">/**
     * &#x8FD4;&#x56DE;&#x5BF9;&#x8C61;&#x7C7B;&#x578B;
     */</span>
    <span class="hljs-keyword">private</span> String returnType;

    <span class="hljs-comment">/**
     * &#x5F02;&#x5E38;
     */</span>
    <span class="hljs-keyword">private</span> String exception;


    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">RpcResponse</span><span class="hljs-params">(String uuid)</span> </span>{
        <span class="hljs-keyword">this</span>.uuid = uuid;
    }

   getX/setX()

}
</code></pre>
<p>&#x901A;&#x8FC7;&#x4E0A;&#x8FF0;&#x5185;&#x5BB9;&#x4FBF;&#x5B8C;&#x6210;RPC&#x901A;&#x4FE1;&#x7684;&#x6D88;&#x606F;&#x8BBE;&#x8BA1;&#xFF0C;&#x81F3;&#x4E8E;RpcRequest&#x3001;RpcResponse&#x5982;&#x4F55;&#x8F6C;&#x5316;&#x4E3A;&#x901A;&#x4FE1;&#x534F;&#x8BAE;&#x8981;&#x6C42;&#x7684;byte&#x6570;&#x7EC4;&#x683C;&#x5F0F;&#xFF0C;&#x6211;&#x4EEC;&#x91C7;&#x7528;JDK&#x63D0;&#x4F9B;&#x7684;&#x5E8F;&#x5217;&#x5316;&#x65B9;&#x5F0F;&#xFF08;&#x751F;&#x4EA7;&#x73AF;&#x5883;&#x4E0D;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528;&#xFF09;&#x3002;</p>
<ul>
<li>&#x5E8F;&#x5217;&#x5316;<pre><code>ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutput objectOutput = new ObjectOutputStream(byteArrayOutputStream);
objectOutput.writeObject(request);
aioSession.write(byteArrayOutputStream.toByteArray());
</code></pre></li>
<li>&#x53CD;&#x5E8F;&#x5217;&#x5316;<pre><code>ObjectInputStream objectInput = new ObjectInputStream(new ByteArrayInputStream(msg));
RpcResponse resp = (RpcResponse) objectInput.readObject();
</code></pre></li>
</ul>
<h2 id="rpc&#x670D;&#x52A1;&#x5B9E;&#x73B0;">RPC&#x670D;&#x52A1;&#x5B9E;&#x73B0;</h2>
<p>&#x901A;&#x8FC7;&#x4E0A;&#x6587;&#x65B9;&#x6848;&#x6211;&#x4EEC;&#x89E3;&#x51B3;&#x4E86;RPC&#x7684;&#x901A;&#x4FE1;&#x95EE;&#x9898;&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x4FBF;&#x5F97;&#x6839;&#x636E;&#x901A;&#x4FE1;&#x6D88;&#x606F;&#x5B9E;&#x73B0;&#x670D;&#x52A1;&#x80FD;&#x529B;&#x3002;</p>
<h3 id="consumer">Consumer</h3>
<p>&#x7531;&#x4E8E;RPC&#x7684;Consumer&#x7AEF;&#x53EA;&#x6709;&#x63A5;&#x53E3;&#xFF0C;&#x6CA1;&#x6709;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#xFF0C;&#x4F46;&#x5728;&#x4F7F;&#x7528;&#x4E0A;&#x6211;&#x4EEC;&#x53C8;&#x671F;&#x671B;&#x8DDF;&#x672C;&#x5730;&#x670D;&#x52A1;&#x6709;&#x540C;&#x6837;&#x7684;&#x4F7F;&#x7528;&#x4F53;&#x9A8C;&#x3002;
&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5C06;&#x63A5;&#x53E3;&#x5B9E;&#x4F8B;&#x5316;&#x6210;&#x5BF9;&#x8C61;&#xFF0C;&#x5E76;&#x4F7F;&#x5176;&#x5177;&#x5907;&#x8DE8;&#x5E94;&#x7528;&#x670D;&#x52A1;&#x80FD;&#x529B;&#xFF0C;&#x6B64;&#x5904;&#x4FBF;&#x8FD0;&#x7528;&#x5230;&#x52A8;&#x6001;&#x4EE3;&#x7406;&#x3002;
&#x5F53;Consumer&#x8C03;&#x7528;RPC&#x63A5;&#x53E3;&#x65F6;&#xFF0C;&#x4EE3;&#x7406;&#x7C7B;&#x5185;&#x90E8;&#x53D1;&#x9001;&#x8BF7;&#x6C42;&#x6D88;&#x606F;&#x81F3;Provider&#x5E76;&#x83B7;&#x53D6;&#x7ED3;&#x679C;&#x3002;</p>
<pre><code>obj = (T) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{remoteInterface},
        new InvocationHandler() {

            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                RpcRequest req = new RpcRequest();
                req.setInterfaceClass(remoteInterface.getName());
                req.setMethod(method.getName());
                Class&lt;?&gt;[] types = method.getParameterTypes();
                if (!ArrayUtils.isEmpty(types)) {
                    String[] paramClass = new String[types.length];
                    for (int i = 0; i &lt; types.length; i++) {
                        paramClass[i] = types[i].getName();
                    }
                    req.setParamClassList(paramClass);
                }
                req.setParams(args);

                RpcResponse rmiResp = sendRpcRequest(req);
                if (StringUtils.isNotBlank(rmiResp.getException())) {
                    throw new RuntimeException(rmiResp.getException());
                }
                return rmiResp.getReturnObject();
            }
        });
</code></pre><h3 id="provider">Provider</h3>
<p>Provider&#x53EF;&#x5C06;&#x5176;&#x63D0;&#x4F9B;&#x7684;RPC&#x670D;&#x52A1;&#x7EF4;&#x62A4;&#x5728;&#x96C6;&#x5408;&#x91CC;&#xFF0C;&#x91C7;&#x7528;Map&#x5B58;&#x50A8;&#x5373;&#x53EF;&#xFF0C;key&#x4E3A;&#x66B4;&#x9732;&#x7684;&#x63A5;&#x53E3;&#x540D;&#xFF0C;value&#x4E3A;&#x63A5;&#x53E3;&#x7684;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x3002;
&#x4E00;&#x65E6;Provider&#x63A5;&#x53D7;&#x5230;RPC&#x7684;&#x8BF7;&#x6C42;&#x6D88;&#x606F;&#xFF0C;&#x53EA;&#x9700;&#x6839;&#x636E;&#x8BF7;&#x6C42;&#x6D88;&#x606F;&#x5185;&#x5BB9;&#x627E;&#x5230;&#x5E76;&#x6267;&#x884C;&#x5BF9;&#x5E94;&#x7684;&#x670D;&#x52A1;&#xFF0C;&#x6700;&#x540E;&#x5C06;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x4EE5;&#x6D88;&#x606F;&#x7684;&#x5F62;&#x5F0F;&#x8FD4;&#x56DE;&#x81F3;Consumer&#x5373;&#x53EF;&#x3002;</p>
<pre><code>ObjectInputStream objectInput = new ObjectInputStream(new ByteArrayInputStream(msg));
RpcRequest req = (RpcRequest) objectInput.readObject();

RpcResponse resp = new RpcResponse(req.getUuid());
try {
    String[] paramClassList = req.getParamClassList();
    Object[] paramObjList = req.getParams();
    // &#x83B7;&#x53D6;&#x5165;&#x53C2;&#x7C7B;&#x578B;
    Class&lt;?&gt;[] classArray = null;
    if (paramClassList != null) {
        classArray = new Class[paramClassList.length];
        for (int i = 0; i &lt; classArray.length; i++) {
            Class&lt;?&gt; clazz = primitiveClass.get(paramClassList[i]);
            if (clazz == null) {
                classArray[i] = Class.forName(paramClassList[i]);
            } else {
                classArray[i] = clazz;
            }
        }
    }
    // &#x8C03;&#x7528;&#x63A5;&#x53E3;
    Object impObj = impMap.get(req.getInterfaceClass());
    if (impObj == null) {
        throw new UnsupportedOperationException(&quot;can not find interface: &quot; + req.getInterfaceClass());
    }
    Method method = impObj.getClass().getMethod(req.getMethod(), classArray);
    Object obj = method.invoke(impObj, paramObjList);
    resp.setReturnObject(obj);
    resp.setReturnType(method.getReturnType().getName());
} catch (InvocationTargetException e) {
    LOGGER.error(e.getMessage(), e);
    resp.setException(e.getTargetException().getMessage());
} catch (Exception e) {
    LOGGER.error(e.getMessage(), e);
    resp.setException(e.getMessage());
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
objectOutput = new ObjectOutputStream(byteArrayOutputStream);
objectOutput.writeObject(resp);
session.write(byteArrayOutputStream.toByteArray());
</code></pre><h2 id="&#x6D4B;&#x8BD5;rpc&#x670D;&#x52A1;">&#x6D4B;&#x8BD5;RPC&#x670D;&#x52A1;</h2>
<p>&#x670D;&#x52A1;&#x7AEF;&#x5B9A;&#x4E49;&#x63A5;&#x53E3;<code>DemoApi</code>&#xFF0C;&#x5E76;&#x5C06;&#x5176;&#x5B9E;&#x73B0;&#x793A;&#x4F8B;<code>DemoApiImpl</code>&#x6CE8;&#x518C;&#x81F3;Provider&#x4E2D;&#x3002;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Provider</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> IOException </span>{
        RpcProviderProcessor rpcProviderProcessor = <span class="hljs-keyword">new</span> RpcProviderProcessor();
        AioQuickServer&lt;<span class="hljs-keyword">byte</span>[]&gt; server = <span class="hljs-keyword">new</span> AioQuickServer&lt;&gt;(<span class="hljs-number">8888</span>, <span class="hljs-keyword">new</span> RpcProtocol(), rpcProviderProcessor);
        server.start();

        rpcProviderProcessor.publishService(DemoApi.class, <span class="hljs-keyword">new</span> DemoApiImpl());
    }
}
</code></pre>
<p>Consumer&#x8C03;&#x7528;RPC&#x63A5;&#x53E3;<code>test</code>&#x3001;<code>sum</code>&#x83B7;&#x5F97;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x3002;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Consumer</span> </span>{

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> InterruptedException, ExecutionException, IOException </span>{

        RpcConsumerProcessor rpcConsumerProcessor = <span class="hljs-keyword">new</span> RpcConsumerProcessor();
        AioQuickClient&lt;<span class="hljs-keyword">byte</span>[]&gt; consumer = <span class="hljs-keyword">new</span> AioQuickClient&lt;&gt;(<span class="hljs-string">&quot;localhost&quot;</span>, <span class="hljs-number">8888</span>, <span class="hljs-keyword">new</span> RpcProtocol(), rpcConsumerProcessor);
        consumer.start();

        DemoApi demoApi = rpcConsumerProcessor.getObject(DemoApi.class);
        System.out.println(demoApi.test(<span class="hljs-string">&quot;smart-socket&quot;</span>));
        System.out.println(demoApi.sum(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>));
    }

}
</code></pre>
<h2 id="&#x603B;&#x7ED3;">&#x603B;&#x7ED3;</h2>
<p>&#x672C;&#x6587;&#x7B80;&#x8981;&#x63CF;&#x8FF0;&#x4E86;RPC&#x670D;&#x52A1;&#x5B9E;&#x73B0;&#x7684;&#x5173;&#x952E;&#x90E8;&#x5206;&#xFF0C;&#x4F46;&#x662F;&#x63D0;&#x4F9B;&#x7A33;&#x5B9A;&#x53EF;&#x9760;&#x7684;RPC&#x670D;&#x52A1;&#x8FD8;&#x6709;&#x5F88;&#x591A;&#x7EC6;&#x8282;&#x9700;&#x8981;&#x8003;&#x8651;&#xFF0C;&#x6709;&#x5174;&#x8DA3;&#x7684;&#x670B;&#x53CB;&#x53EF;&#x81EA;&#x884C;&#x7814;&#x7A76;&#x3002;
&#x672C;&#x6587;&#x793A;&#x4F8B;&#x7684;&#x5B8C;&#x6574;&#x4EE3;&#x7801;&#x53EF;&#x4ECE;smart-socket&#x9879;&#x76EE;&#x4E2D;&#x83B7;&#x53D6;&#x3002; </p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; smartboot.org 2018 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2018-07-01 19:49:04
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                
                <a href="../firstfit/readme.html" class="navigation navigation-next navigation-unique" aria-label="Next page: 内存管理-首次适应算法">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"smart-socket实现RPC","level":"1.4.1","depth":2,"next":{"title":"内存管理-首次适应算法","level":"1.4.2","depth":2,"path":"share/firstfit/readme.md","ref":"share/firstfit/readme.md","articles":[]},"previous":{"title":"技术分享","level":"1.4","depth":1,"ref":"","articles":[{"title":"smart-socket实现RPC","level":"1.4.1","depth":2,"path":"share/rpc/smart-socket-rpc.md","ref":"share/rpc/smart-socket-rpc.md","articles":[]},{"title":"内存管理-首次适应算法","level":"1.4.2","depth":2,"path":"share/firstfit/readme.md","ref":"share/firstfit/readme.md","articles":[]},{"title":"Thread知多少","level":"1.4.3","depth":2,"path":"share/thread/readme.md","ref":"share/thread/readme.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","-sharing","search-plus","expandable-chapters-interactive","tbfed-pagefooter","github","github-buttons","edit-link"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy smartboot.org 2018","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"github":{"url":"https://github.com/smartboot/smart-socket"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"expandable-chapters-interactive":{},"github-buttons":{"buttons":[{"user":"smartboot","repo":"smart-socket","type":"star","size":"small","count":true}]},"edit-link":{"label":"Edit This Page","base":"https://gitee.com/smartboot/book/edit/master"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"search-plus":{}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"smartboot 中文指南","language":"en","gitbook":"*","description":"本文涵盖了smartboot下所有项目的技术手册"},"file":{"path":"share/rpc/smart-socket-rpc.md","mtime":"2018-07-01T11:49:04.013Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2020-04-02T05:22:17.739Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

